import { getProductListApi, getProductDetailApi } from '@/api'
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'

// 获得产品列表
export const fetchProductListAction = createAsyncThunk(
  'product/fetchProductListAction',
  async (_, { dispatch }) => {
    const res = await getProductListApi()
    dispatch(setProductList(res))
  }
)
// 获得产品详情
export const fetchProductDatailAction = createAsyncThunk(
  'product/fetchProductDetailAction',
  async (id: number | string) => {
    const res = await getProductDetailApi(id)
    return res
  }
)

interface IState {
  productList: any[]
}

const initialState: IState = {
  productList: []
}

const productReducer = createSlice({
  name: 'home',
  initialState: initialState,
  reducers: {
    setProductList(state, action) {
      state.productList = action.payload
    }
  }
})
export const { setProductList } = productReducer.actions
export default productReducer.reducer
